#!/usr/bin/env python
'''Find python module version'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
__license__ = "BSD" # see LICENSE

def valueof(v):
    if callable(v):
        try:
            return v()
        except Exception:
            return None
    return v

def find_module_version(module_name):
    module = __import__(module_name)
    attrs = set(dir(module))

    for known in ("__version__", "version"):
        if known in attrs:
            v = valueof(getattr(module, known))
            if v:
                return v

    for attr in attrs:
        if "version" in attr.lower():
            v = getattr(module, attr)
            print attr
            if not v:
                continue
            v = valueof(v)
            if v:
                return v

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from optparse import OptionParser

    parser = OptionParser("usage: %prog MODULE_NAME")

    opts, args = parser.parse_args(argv[1:])
    if len(args) != 1:
        parser.error("wrong number of arguments") # Will exit

    module_name = args[0]

    try:
        version = find_module_version(module_name)
    except ImportError, e:
        raise SystemExit("error: can't import %s (%s)" % (module_name, e))

    if version:
        print version
    else:
        raise SystemExit("error: can't find version for %s" % module_name)

if __name__ == "__main__":
    main()

